package com.project.controller.web;


import com.project.entity.Class;
import com.project.entity.Major;
import com.project.myBeans.R;
import com.project.service.ClassService;
import com.project.service.MajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@EnableAutoConfiguration
@RestController
@Api(description = "班级接口",value = "class")
@RequestMapping("/class")
public class ClassController {

    @Autowired
    ClassService classService;

    @Autowired
    MajorService majorService;

    @ApiOperation("添加班级")
    @PostMapping("/add")
    public R add(@RequestBody Class clazz){
        try{
            if(classService.findByCondition(clazz).size()!=0){
                return R.error("已存在该班级");
            }else {
                Major major = majorService.findById(clazz.getMajorid());
                clazz.setDepartmentid(major.getDepartmentid());
                clazz.setSchoolid(major.getSchoolid());
                clazz.setUniversityid(major.getUniversityid());
                classService.add(clazz);
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Tate 2018/5/23
     *
     * @param majorid
     * @return
     */
    @ApiOperation("根据专业查找班级")
    @PostMapping("/findclassbymajorid/{majorid}")
    public R findclassbymajorid(@PathVariable Integer majorid) {
        try {
            List<Class> list = classService.findByMajorId(majorid);
            return R.ok().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
}
